# frozen_string_literal: true

# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.

# Load "path" as a rackup file.
#
# The default is "config.ru".
#
environment '<%= @environment %>'
rackup '<%= @install_dir %>/embedded/service/gitlab-rails/<%= @rackup %>'
pidfile '<%= @pid %>'
state_path '<%= @state_path %>'

stdout_redirect '<%= @stdout_path %>', '<%= @stderr_path %>', true

# Configure "min" to be the minimum number of threads to use to answer
# requests and "max" the maximum.
#
# The default is "0, 16".
#
threads <%= @min_threads %>, <%= @max_threads %>

# By default, workers accept all requests and queue them to pass to handlers.
# When false, workers accept the number of simultaneous requests configured.
#
# Queueing requests generally improves performance, but can cause deadlocks if
# the app is waiting on a request to itself. See https://github.com/puma/puma/issues/612
#
# When set to false this may require a reverse proxy to handle slow clients and
# queue requests before they reach puma. This is due to disabling HTTP keepalive
queue_requests false

# Bind the server to "url". "tcp://", "unix://" and "ssl://" are the only
# accepted protocols.
<%- if @listen_socket && !@listen_socket.empty? %>
bind 'unix://<%= @listen_socket %>'
<%- end %>

<%- if @listen_tcp %>
bind 'tcp://<%= @listen_tcp %>'
<%- end %>

<%- if @ssl_listen_host && @ssl_port %>
ssl_bind '<%= @ssl_listen_host %>', <%= @ssl_port %>, {
  <%- if @ssl_certificate %>
  cert: '<%= @ssl_certificate %>',
  <%- end %>
  <%- if @ssl_certificate_key %>
  key: '<%= @ssl_certificate_key %>',
  <%- end %>
  <%- if @ssl_key_password_command %>
  key_password_command: '<%= @ssl_key_password_command %>',
  <%- end %>
  <%- if @ssl_client_certificate %>
  ca: '<%= @ssl_client_certificate %>',
  <%- end %>
  <%- if @ssl_cipher_filter %>
  ssl_cipher_filter: '<%= @ssl_cipher_filter %>',
  <%- end %>
  <%- if @ssl_verify_mode %>
  verify_mode: '<%= @ssl_verify_mode %>'
  <%- end %>
}
<%- end %>

<%- if @working_directory %>
directory '<%= @working_directory %>'
<%- end %>

workers <%= @worker_processes %>

require_relative "<%= @install_dir %>/embedded/service/gitlab-rails/lib/gitlab/cluster/lifecycle_events"

on_restart do
  # Signal application hooks that we're about to restart
  Gitlab::Cluster::LifecycleEvents.do_before_master_restart
end

options = { workers: <%= @worker_processes %> }

before_fork do
  # Signal application hooks that we're about to fork
  Gitlab::Cluster::LifecycleEvents.do_before_fork
end

Gitlab::Cluster::LifecycleEvents.set_puma_options options
on_worker_boot do
  # Signal application hooks of worker start
  Gitlab::Cluster::LifecycleEvents.do_worker_start
end

on_worker_shutdown do
  # Signal application hooks that a worker is shutting down
  Gitlab::Cluster::LifecycleEvents.do_worker_stop
end

# Preload the application before starting the workers; this conflicts with
# phased restart feature. (off by default)

preload_app!

tag '<%= @tag %>'

# Verifies that all workers have checked in to the master process within
# the given timeout. If not the worker process will be restarted. Default
# value is 60 seconds.
#
worker_timeout <%= @worker_timeout %>

# https://github.com/puma/puma/blob/master/5.0-Upgrade.md#lower-latency-better-throughput
wait_for_less_busy_worker ENV.fetch('PUMA_WAIT_FOR_LESS_BUSY_WORKER', 0.001).to_f

# nakayoshi_fork was removed in Puma 6.0: https://github.com/puma/puma/issues/2258
# https://github.com/puma/puma/blob/master/5.0-Upgrade.md#nakayoshi_fork
if Gem::Version.new(Puma::Const::PUMA_VERSION).canonical_segments.first == 5 &&
     ENV['DISABLE_PUMA_NAKAYOSHI_FORK'] != 'true'
  nakayoshi_fork
end

# Use customised JSON formatter for Puma log
require_relative "<%= @install_dir %>/embedded/service/gitlab-rails/lib/gitlab/puma_logging/json_formatter"

json_formatter = Gitlab::PumaLogging::JSONFormatter.new
log_formatter do |str|
    json_formatter.call(str)
end

lowlevel_error_handler do |ex, env|
  if Raven.configuration.capture_allowed?
    Raven.capture_exception(ex, tags: { 'handler': 'puma_low_level' }, extra: { puma_env: env })
  end

  # note the below is just a Rack response
  [500, {}, ["An error has occurred and reported in the system's low-level error handler."]]
end
